/**
 * 
 */
package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.wyge.relations.adataset.bookmark.DatasetBookmark;

/**
 * @author Weiyi Ge (geweiyi@gmail.com) 2012-7-23
 * 
 */
public class BookmarkInsert extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BookmarkInsert() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String json = null;
		response.setCharacterEncoding("utf-8");
		if (request.getParameter("insert") != null) {
			json = request.getParameter("insert");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			String bookmarkName = URLDecoder.decode(
					(String) regtJSON.get("bookmarkName"), "UTF-8");
			String userName = (String) regtJSON.get("userName");
			String ds_type = (String) regtJSON.get("ds_type");
			if(ds_type.contentEquals("uri") || ds_type.contentEquals("custom")){
				int ds_id = Integer.parseInt((String) regtJSON.get("ds_id"));
				String lang = (String) regtJSON.get("lang");
				try {
					DatasetBookmark.insert(bookmarkName, userName, ds_type, ds_id, lang);
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				System.err.println("wrong ds_type value! bookmark insert failed! at servlet BookmarkInsert: ds_type="+ds_type);
			}
		}
	}
}
